import cv2
image=cv2.imread('hat.png',cv2.IMREAD_GRAYSCALE)
kerne1=cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))
tophat=cv2.morphologyEx(image,cv2.MORPH_GRADIENT,kerne1)
blackhat=cv2.morphologyEx(image,cv2.MORPH_BLACKHAT,kerne1)
cv2.imshow("Input",image)
cv2.imshow("TopHat",tophat)
cv2.imshow("BlackHat",blackhat)
cv2.waitKey()
cv2.destroyAllWindows()